using Consts;
using Dto;
using Model;
using Newtonsoft.Json;
using Scenes;
using Scenes.Msg;
using UI;
using UI.Msg;
using UnityEngine;

namespace Net.Handler
{
    public class AccountHandler : HandlerBase
    {
        public override void OnReceive(ResBase resBase)
        {
            switch (resBase.subCode)
            {
                case AccountCodeConst.LOGIN:
                    login(resBase);
                    break;
                case AccountCodeConst.REGIST:
                    regist(resBase);
                    break;
                case AccountCodeConst.GET_USER:
                    GetUser(resBase);
                    break;
                case AccountCodeConst.SET_NAME:
                    SetName(resBase);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 设置用户昵称
        /// </summary>
        /// <param name="resBase"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void SetName(ResBase resBase)
        {
            GameModel gameModel = Models.GameModel;
            gameModel.Account.nickName = resBase.result.ToString();
            Dispatch(AreaCode.UI,UIEvent.CREATE_PANEL_ACTIVE,false);
            Dispatch(AreaCode.UI,UIEvent.REFRESH_INFO_PANEL,null);
        }

        private void GetUser(ResBase resBase)
        {
            Account account = JsonConvert.DeserializeObject<Account>(resBase.result.ToString());
            GameModel gameModel = Models.GameModel;
            gameModel.Account = account;
            if (string.IsNullOrEmpty(account.nickName))
            {
                // 名称为null 创建角色
                Dispatch(AreaCode.UI,UIEvent.CREATE_PANEL_ACTIVE,true);
            }
            else
            {
                // 名称不为null 隐藏创建角色
                Dispatch(AreaCode.UI,UIEvent.CREATE_PANEL_ACTIVE,false);
                Dispatch(AreaCode.UI,UIEvent.REFRESH_INFO_PANEL,null);
            }
        }

        private void login(ResBase resBase)
        {
            if (resBase.resCode == 200)
            {
                PromptMsg _promptMsg = new PromptMsg(resBase.result.ToString(), Color.green);
                MsgCenter.Instance.Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, _promptMsg);
                LoadSceneMsg loadSceneMsg = new LoadSceneMsg(1, () =>
                {
                    // TODO
                    // 向服务器获取信息
                    LoadUser();
                    Debug.Log("登录加载完成!");
                });
                Dispatch(AreaCode.SCENE, SceneEvent.LOAD_SCENE, loadSceneMsg);
            }
        }


        private void regist(ResBase resBase)
        {
            if (resBase.resCode == 200)
            {
                PromptMsg _promptMsg = new PromptMsg(resBase.result.ToString(), Color.green);
                MsgCenter.Instance.Dispatch(AreaCode.UI, UIEvent.PROMPT_MSG, _promptMsg);
                LoadSceneMsg loadSceneMsg = new LoadSceneMsg(1, () =>
                {
                    LoadUser();
                    Debug.Log("注册加载完成!");
                });
                Dispatch(AreaCode.SCENE, SceneEvent.LOAD_SCENE, loadSceneMsg);
            }
        }

        private void LoadUser()
        {
            ReqBase _reqBase = new ReqBase(SocketConst.ACCOUNT, AccountCodeConst.GET_USER);
            Dispatch(AreaCode.NET, 0, _reqBase);
        }
    }
}